package trabalho.doo3.j2me.bean;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Renan
 */
public class Reserva {

    private int id;
    private String user_id;
    private String filme_id;
    private String deletar;
    private static final String RMSSTORE = "RMSRESERVA";
    private RecordStore recordStore;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getFilme_id() {
        return filme_id;
    }

    public void setFilme_id(String filme_id) {
        this.filme_id = filme_id;
    }

    public String getDeletar() {
        return deletar;
    }

    public void setDeletar(String deletar) {
        this.deletar = deletar;
    }

    public RecordStore getRecordStore() {
        return recordStore;
    }

    public void setRecordStore(RecordStore recordStore) {
        this.recordStore = recordStore;
    }

    public byte[] getBytes() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeUTF(filme_id);
        dos.writeUTF(deletar);
        byte[] ret = baos.toByteArray();
        dos.close();
        baos.close();
        return ret;
    }

    public void setBytes(byte[] valores) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(valores);
        DataInputStream dis = new DataInputStream(bais);
        this.filme_id = dis.readUTF();
        this.deletar = dis.readUTF();
        dis.close();
        bais.close();
    }

    public void open() throws RecordStoreException {
        this.recordStore = RecordStore.openRecordStore(RMSSTORE, true);
    }

    public void close() throws RecordStoreNotOpenException, RecordStoreException {
        recordStore.closeRecordStore();
    }

    public void clear() throws RecordStoreException {
        RecordStore.deleteRecordStore(RMSSTORE);
    }

    public void insert(Reserva lista) throws IOException, RecordStoreNotOpenException, RecordStoreException {
        byte[] registroBytes = lista.getBytes();
        int id = recordStore.addRecord(registroBytes, 0, registroBytes.length);
        lista.setId(id);
    }

    public void delete(Reserva lista) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException {
        recordStore.deleteRecord(lista.getId());
    }

    public void update(Reserva lista) throws IOException, RecordStoreNotOpenException, RecordStoreException {
        byte[] registroBytes = lista.getBytes();
        recordStore.setRecord(lista.getId(), registroBytes, 0, registroBytes.length);
    }

    public Vector todos() throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
        Vector ret = new Vector();
        RecordEnumeration enumerator = recordStore.enumerateRecords(null, null, false);
        while (enumerator.hasNextElement()) {
            Reserva listaBean = new Reserva();
            listaBean.setId(enumerator.nextRecordId());
            listaBean.setBytes(recordStore.getRecord(listaBean.getId()));
            ret.addElement(listaBean);
        }
        return ret;
    }
}
